const { join, resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;


module.exports = {
    mode: "development",
    output: {
        assetModuleFilename: "images/[name].[contenthash:5].bundle.[ext]",
        filename: "scripts/[name].[contenthash:5].bundle.js",
        publicPath: '/'
    },
    optimization: {
        minimize: true,
        sideEffects:false,
        minimizer: [
            new CssMinimizerPlugin(),
        ],
        runtimeChunk: {
            name: "runtime"
        },
        splitChunks: {
            chunks: "async",
            minChunks: 3,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            name: false,
            cacheGroups: {
                commons: {
                    chunks: "initial",
                    minChunks: 2,
                    name: "commons"
                }
            },
            minSize: {
                javascript: 100000,
                style: 100000
            }
        }
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: "智慧怀化",
            filename: "index.html",
            template: resolve(__dirname, "../src/index-prod.html"),
            minify: {
                removeComments: true,
                collapseBooleanAttributes: true,
                removeAttributeQuotes: true
            }
        }),
        new MiniCssExtractPlugin(),
        new BundleAnalyzerPlugin(),
        // new OptimizeCSSAssetsPlugin({
        //     assetNameRegExp:/\.css$/g,
        //     cssProcessor:require('cssnano'),
        //     cssProcessorPluginOptions:{
        //         preset: [
        //             "default",
        //             {
        //                 discardComments:{
        //                     removeAll:true
        //                 }
        //             }
        //         ]
        //     }
        // })
    ]
}